package com.shop.seckill.controller;

import com.base.common.entity.SeckillGoods;
import com.base.common.entity.SeckillOrder;
import com.base.common.entity.User;
import com.base.common.http.HttpResult;
import com.base.common.http.HttpStatus;
import com.base.common.utils.SeckillStatus;
import com.base.common.utils.UserUtils;
import com.shop.seckill.service.SecKillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/seckill/order")
public class SeckillOrderController {

    @Autowired
    private SecKillOrderService secKillOrderService;

    /****
     * 添加秒杀订单
     * @param seckillId : 秒杀的商品ID
     */
    @RequestMapping("/add")
    public HttpResult add(@RequestParam("seckillId") Long seckillId, HttpServletRequest request){
        User user = UserUtils.getUser(request);
        //判断登录
        if (user == null) {
            return HttpResult.error(HttpStatus.SC_FORBIDDEN,"请先登录!");

        }
        //提交订单
        try {
        HttpResult result = secKillOrderService.addSeckillOrder(String.valueOf(user.getId()), seckillId);
            return result;
        } catch (Exception e){
            e.printStackTrace();
        }
        return HttpResult.ok("添加秒杀订单失败");
    }

    /***
     * 查询用户秒杀排队信息
     * url:/seckill/order/status
     * 秒杀的商品ID
     * 调用Service查询下单状态
     * @param id:秒杀查询的商品ID
     */
    @RequestMapping(value = "/status")
    public HttpResult status(@RequestParam("id") Long id, HttpServletRequest request){
        User user = UserUtils.getUser(request);
        //判断登录
        if (user == null) {
            return HttpResult.error(HttpStatus.SC_FORBIDDEN,"请先登录!");
        }
        //秒杀状态  1:排队中，2:秒杀等待支付,3:支付超时，4:秒杀失败,5:支付完成

        //调用状态查询
        SeckillStatus seckillStatus = secKillOrderService.getStatusByUserId(String.valueOf(user.getId()));

        //如果此时seckillStatus==null  || 查询的商品ID!=排队的商品ID
        if(seckillStatus==null || seckillStatus.getGoodsId().longValue()!=id.longValue()){
            //没有排队信息
            return HttpResult.error(HttpStatus.SC_NOT_FOUND,"没有排队信息");
        }else if(seckillStatus.getStatus()==2){
            //如果状态为2，抢购成功，待支付
            return HttpResult.ok("抢购成功，请支付");
        }

        //其他状态
        return HttpResult.error(HttpStatus.SC_EXPECTATION_FAILED,String.valueOf(seckillStatus.getStatus()));

    }

    @RequestMapping(value = "/findSecKillOrder")
    public HttpResult findSecKillOrder(HttpServletRequest request) {

        User user = UserUtils.getUser(request);

        //判断登录
        if (user == null) {
            return HttpResult.error(HttpStatus.SC_FORBIDDEN,"请先登录!");
        }

        /** 查看秒杀抢购的商品 */
        SeckillGoods secKillOrder = secKillOrderService.findSecKillOrder(String.valueOf(user.getId()));
        return HttpResult.ok(secKillOrder);
    }


}
